<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .red {
        color: red;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="value" @keyup.enter="add" />
      <button @click="add">新增</button>
      <ul>
        <li v-for="item in list" :key="item.id" :class="{ red: item.checked }">
          <!-- <input type="checkbox" v-model="arr" :value="item.id" /> -->
          <input type="checkbox" v-model="item.checked" />
          - {{ item.name }} -
          <button @click="remove(item.id)">删除</button>
        </li>
      </ul>

      <button @click="removeAll">删除选中</button>
      <!-- <p>{{ arr }}</p> -->
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      Vue.createApp({
        data() {
          return {
            value: "",
            list: [
              {
                name: "zhangsan",
                id: 1,
                checked: false,
              },
              {
                name: "lisi",
                id: 2,
                checked: true,
              },
            ],
            // arr: [],
          };
        },
        methods: {
          add() {
            if (this.value) {
              // console.log(this.value);
              this.list.push({
                name: this.value,
                id: new Date().getTime(),
                checked: false,
              });
              this.value = "";
            }
          },
          remove(id) {
            // console.log("remove", id);
            this.list = this.list.filter((item) => item.id !== id);
          },
          removeAll() {
            // this.list = this.list.filter((item) => !this.arr.includes(item.id));
            // this.arr = [];
            this.list = this.list.filter((item) => !item.checked);
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
